package com.vgooo.client.ui;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.vgooo.client.Main;
import com.vgooo.client.R;
import com.vgooo.client.ViewLayout;
import com.vgooo.client.vchannel.PingDetail;
import com.vgooo.util.BitmapUtils;
import com.vgooo.util.DensityUtil;
import com.vgooo.util.Utils;

public class ViewPagerAdapter extends PagerAdapter implements OnTouchListener ,OnPageChangeListener{

	private ArrayList<View> views;
	private List<String> urls;
	private ViewPager pager;
	private TextView pingButton, allPager, currentPager;
	private String pings;
	private int currentView = 1;
	private static int pingViewIndex;
	private PingDetail pingDetail;
	private boolean hasPing;
	private boolean isAlbum;
	private Object params1;
	private android.widget.RelativeLayout.LayoutParams params2;
	private int p;

	public ViewPagerAdapter(final ViewPager pager, List<View> views,
			TextView pingBotton, String pings, final PingDetail pingDetail,
			List<String> urls) {
		this.pager = pager;
		this.pingButton = pingBotton;
		this.pings = "评论(" + pings + ")";
		this.views = new ArrayList<View>() ;
		this.views.add(new ImageView(pager.getContext()));
		this.views.addAll(views);
		if (null != pingDetail) {
			hasPing = true;
			this.pingDetail = pingDetail;
			this.views.add(pingDetail.getmPullDownView());
			pingBotton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (currentView == pingViewIndex) {
						currentView--;
						setCurrentView();
					} else if (currentView != 0) {
						currentView = pingViewIndex;
						setCurrentView();
					}
				}
			});
		} else {
			hasPing = false;
		}
		
		this.currentView = 1;
		if (null != urls && !urls.isEmpty()) {
			this.urls = urls;
			isAlbum = true;
		} else {
			isAlbum = false;
			setCurrentView();
		}

		pingViewIndex = this.views.size() - 1;
		if (hasPing && isAlbum) {
			p = pager.getPaddingBottom();
			params1 = new android.widget.RelativeLayout.LayoutParams(
					android.widget.RelativeLayout.LayoutParams.FILL_PARENT,
					pager.getLayoutParams().height + 40 * p);
			params2 = new android.widget.RelativeLayout.LayoutParams(
					android.widget.RelativeLayout.LayoutParams.FILL_PARENT,
					480 * p);
			p *= 40;
		}

		this.pager.setOnPageChangeListener(this);
		for(View view: this.views){
			view.setOnTouchListener(this);
		}
		
	}

	private void setCurrentView() {
		if (currentView == 0) {
			currentView = 1;
			ViewLayout.setViewBack();
		} else if (hasPing) {
			if (currentView == pingViewIndex) {
				pingButton.setText("原文");
				Main.title.setText("评论详情");
			} else {
				pingButton.setText(pings);
				Main.title.setText(ViewLayout.viwStack.peek().title);
			}
		}
		if (isAlbum) {
			currentPager.setText(currentView + "");
		}
		pager.setCurrentItem(currentView);
	}

	public void setPagerCount(TextView current, TextView all) {
		this.allPager = all;
		this.currentPager = current;
		all.setText(views.size() - 1 + "");
		currentPager.setText("1");
		setCurrentView();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return views.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0 == arg1;
	}

	@Override
	public void destroyItem(View container, int position, Object object) {
		// TODO Auto-generated method stub
		View view = views.get(position);
		((ViewPager) container).removeView(view);
		if (isAlbum && position > 0 && position < pingViewIndex) {
			ImageView iv = (ImageView) view;
			Bitmap bm = iv.getDrawingCache(true);
			iv.setImageResource(R.drawable.bg_transparent);
			if (bm != null && !bm.isRecycled()) {
				bm.recycle();
			}
		}
	}

	@Override
	public void finishUpdate(ViewGroup container) {
		// TODO Auto-generated method stub
		super.finishUpdate(container);
	}

	@Override
	public int getItemPosition(Object object) {
		// TODO Auto-generated method stub
		return super.getItemPosition(object);
	}

	@Override
	public Object instantiateItem(View container, int position) {
		// TODO Auto-generated method stub
		View view = views.get(position);
		if (isAlbum && position > 0) {
			if (hasPing && position == pingViewIndex) {
				((ViewPager) container).addView(view);
				return view;
			}
			ImageView iv = (ImageView) view;
			position--;
			BitmapUtils.albumLoadPool.LoadImage(iv, urls.get(position));
			((ViewPager) container).addView(iv);
			return iv;
		} else {
			((ViewPager) container).addView(view);
			return view;
		}
	}

	@Override
	public Parcelable saveState() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if(null!=pingDetail&&pingDetail.isEdit){
			Utils.hideSoftInput(pager);
		}
		return false;
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		if (pingDetail != null && pingDetail.isEdit) {
			Utils.hideSoftInput(pager);
		}
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		if (pingDetail != null && pingDetail.isEdit) {
			Utils.hideSoftInput(pager);
		} else {
			currentView = arg0;
			if (hasPing && isAlbum) {
				if (currentView == pingViewIndex) {
					pager.setLayoutParams(params2);
					pager.setPadding(0, 0, 0, 0);
				} else {
					pager.setPadding(0, p, 0, 0);
					pager.setLayoutParams((android.view.ViewGroup.LayoutParams) params1);
				}
			}
		}
		setCurrentView();
	}
	
	
	

}
